package com.tingyukeji.fastpay.login.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.tingyukeji.fastpay.R;
import com.tingyukeji.fastpay.base.BaseActivity;
import com.tingyukeji.fastpay.data.rsa.RSAUtil;
import com.tingyukeji.fastpay.databinding.LoginActivityBinding;
import com.tingyukeji.fastpay.databinding.RegistActivityBinding;
import com.tingyukeji.fastpay.entity.BaseReqEntity;
import com.tingyukeji.fastpay.entity.RegistEntity;
import com.tingyukeji.fastpay.entity.ReqCompletedLisener;
import com.tingyukeji.fastpay.entity.ValidateCodeEntity;
import com.tingyukeji.fastpay.login.utils.TimerCallBack;
import com.tingyukeji.fastpay.login.utils.UserCountTimer;
import com.tingyukeji.fastpay.utils.FastClickUtils;
import com.tingyukeji.fastpay.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2016/11/11.
 */

public class RegistActivity extends BaseActivity implements View.OnClickListener {
    private UserCountTimer countTimer;
    private boolean timerFinish = true;
    private RegistActivityBinding binding;
    private SharedPreferencesUtils sp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        super.setContentView();
        binding = DataBindingUtil.setContentView(this, R.layout.regist_activity);
    }

    @Override
    protected void initView() {
        super.initView();
        sp = new SharedPreferencesUtils(this);
        countTimer = new UserCountTimer(60000, 1000, binding.tvGetSmsCode, "秒后重新发送", "获取验证码");
        countTimer.setTimerCallBackListener(new TimerCallBack.TimerCallBackListener() {
            @Override
            public void onFinish() {
                timerFinish = true;
            }
        });
        binding.btNext.setOnClickListener(this);
        binding.tvGetSmsCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_next:
                if (!FastClickUtils.isFastClick(500)){
                    String mobilePhone = binding.etPhone.getText().toString();
                    String smsVcode = binding.etSmsVcode.getText().toString();
                    if (!TextUtils.isEmpty(mobilePhone) && !TextUtils.isEmpty(smsVcode)){
//                        Bundle bundle = new Bundle();
//                        bundle.putString("mobilePhone",binding.etPhone.getText().toString());
//                        bundle.putString("validateCode",binding.etSmsVcode.getText().toString());
//                        goActivity(SetPwActivity.class,bundle);
                        goRegist(mobilePhone,smsVcode);
                    }else {
                        Toast.makeText(RegistActivity.this,"请输入手机号码和短信验证码",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.tv_get_sms_code:
                if (timerFinish && !FastClickUtils.isFastClick(500)){
                    String phoneNum = binding.etPhone.getText().toString();
                    if (!TextUtils.isEmpty(phoneNum)){
                        getSmsCode(phoneNum);
                    }
                }
                break;
        }
    }

    private void goRegist(String mobilePhone, String validateCode) {
        if (!TextUtils.isEmpty(mobilePhone) && !TextUtils.isEmpty(validateCode)){
            BaseReqEntity registEntity = new RegistEntity()
                    .putParam("mobilePhone",mobilePhone)
                    .putParam("validateCode",validateCode)
                    .setReqType(BaseReqEntity.POST)
                    .setLisener(new ReqCompletedLisener<RegistEntity>() {
                        @Override
                        public void notifyReqCompleted(boolean success, RegistEntity data, String errMsg, boolean isCacheOpen) {
                            if (null != data) {
                                if (data.code == 1){
                                    sp.setParam("TOKEN", data.getData().token);
                                    goActivity(SetPwActivity.class);
                                }
                                Toast.makeText(RegistActivity.this,data.message,Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(RegistActivity.this,"请求失败！",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
            registEntity.execute();
        }
    }

    private void getSmsCode(String phoneNum) {
        timerFinish = false;
        countTimer.start();
        BaseReqEntity validateCodeEntity = new ValidateCodeEntity()
                .putParam("mobilePhone",phoneNum)
                .putParam("type",1)
                .setReqType(BaseReqEntity.GET)
                .setLisener(new ReqCompletedLisener<ValidateCodeEntity>() {
                    @Override
                    public void notifyReqCompleted(boolean success, ValidateCodeEntity data, String errMsg, boolean isCacheOpen) {
                        if (null != data) {
                            if (data.successful){
                                binding.etSmsVcode.setText(data.getData().validateCode);
                            }
                            Toast.makeText(RegistActivity.this,data.message,Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RegistActivity.this,"请求失败！",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        validateCodeEntity.execute();

    }
}
